package com.hui.controller;

import com.hui.pojo.*;
import com.hui.pojo.vo.ItemInfoVO;
import com.hui.pojo.vo.ShopcartVO;
import com.hui.service.ItemService;
import com.hui.utils.MyJSONResult;
import com.hui.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "商品接口", tags = {"商品信息的相关接口"})
@RestController
@Slf4j
@RequestMapping("items")
public class ItemsController extends BaseController {
    @Autowired
    private ItemService itemsService;

    @ApiOperation(value = "查询商品详情", notes = "查询商品详细数据", httpMethod = "GET")
    @GetMapping("/info/{itemId}")
    public MyJSONResult info(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @PathVariable String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return MyJSONResult.errorMsg("参数出错");
        }
        Items items = itemsService.queryItemById(itemId);
        List<ItemsImg> itemsImgList = itemsService.queryItemImgList(itemId);
        ItemsParam itemsParam = itemsService.queryItemParam(itemId);
        List<ItemsSpec> itemsSpecList = itemsService.queryItemSpecList(itemId);

        ItemInfoVO itemInfoVO = new ItemInfoVO();
        itemInfoVO.setItem(items);
        itemInfoVO.setItemImgList(itemsImgList);
        itemInfoVO.setItemParams(itemsParam);
        itemInfoVO.setItemSpecList(itemsSpecList);

        return MyJSONResult.ok(itemInfoVO);
    }

    @ApiOperation(value = "查询商品等级", notes = "查询商品评价等级", httpMethod = "GET")
    @GetMapping("/commentLevel")
    public MyJSONResult commentLevel(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId) {
        if (StringUtils.isBlank(itemId)) {
            return MyJSONResult.errorMsg("参数出错");
        }
        CommentLevelCountsVO commentLevelCountsVO = itemsService.queryCommentsCounts(itemId);
        return MyJSONResult.ok(commentLevelCountsVO);
    }

    @ApiOperation(value = "查询商品评论", notes = "查询商品评论分页", httpMethod = "GET")
    @GetMapping("/comments")
    public MyJSONResult comments(
            @ApiParam(name = "itemId", value = "商品id", required = true)
            @RequestParam String itemId,
            @ApiParam(name = "level", value = "评价等级", required = false)
            @RequestParam Integer level,
            @ApiParam(name = "page", value = "当前页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示条数", required = false)
            @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(itemId)) {
            return MyJSONResult.errorMsg("参数出错");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMENTS_SIZE;
        }
        PagedGridResult grid = itemsService.queryPagedComments(itemId, level, page, pageSize);
        return MyJSONResult.ok(grid);
    }

    @ApiOperation(value = "搜索商品列表", notes = "搜索商品列表", httpMethod = "GET")
    @GetMapping("/search")
    public MyJSONResult search(
            @ApiParam(name = "keywords", value = "关键字", required = true)
            @RequestParam String keywords,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "当前页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示条数", required = false)
            @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(keywords)) {
            return MyJSONResult.errorMsg("参数出错");
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult grid = itemsService.searchItems(keywords, sort, page, pageSize);
        return MyJSONResult.ok(grid);
    }

    @ApiOperation(value = "搜索商品列表byCatId", notes = "搜索商品列表byCatId", httpMethod = "GET")
    @GetMapping("/catItems")
    public MyJSONResult catItems(
            @ApiParam(name = "catId", value = "catId", required = true)
            @RequestParam Integer catId,
            @ApiParam(name = "sort", value = "排序", required = false)
            @RequestParam String sort,
            @ApiParam(name = "page", value = "当前页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示条数", required = false)
            @RequestParam Integer pageSize) {
        if (catId == null) {
            return MyJSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = PAGE_SIZE;
        }
        PagedGridResult grid = itemsService.searchItems(catId, sort, page, pageSize);
        return MyJSONResult.ok(grid);
    }

    //用户长时间未登录 刷新购物车中的数据
    @ApiOperation(value = "商品规格ids 查找最新的商品数据", notes = "商品规格ids 查找最新的商品数据", httpMethod = "GET")
    @GetMapping("/refresh1")
    public MyJSONResult refresh(
            @ApiParam(name = "itemSpecIds", value = "拼接规格ids", required = true)
            @RequestParam String itemSpecIds) {
        if (itemSpecIds == null) {
            return MyJSONResult.ok();
        }
        log.info("itemSpecIds:{}",itemSpecIds);
        List<ShopcartVO> list = itemsService.queryItemsBySpecIds(itemSpecIds);
        return MyJSONResult.ok(list);
    }
}
